Report post

How to find a class in jQuery?

function hasClass (element, className) { return (' ' + element.className + ' ').indexOf (' ' + className+ ' ') > -1; } Otherwise you will also get true if the class you are looking for is part of another class name. jQuery uses a similar (if not the same) method.

How to check if an element has a specified class name?

Otherwise, it will return true if the class you are looking for is part of another class name. The hasClass () method checks whether any of the selected elements have a specified class name. The method returns true if any of the selected elements has the specified class name. Elements may have multiple classes assigned to them.

What is a ClassName argument in jQuery?

The argument is (obviously)a string representing the class you are checking, and it returns a boolean (so it doesn't support chaining like most jQuery methods). Note: If you pass a className argument that contains whitespace, it will be matched literally against the collection's elements' className string. So if, for instance, you have an element,

How to check if a list of classes contains another string?

className is just a string so you can use the regular indexOf function to see if the list of classes contains another string. What about testing for class class in the above example? From experience, this method can be quite risky: It will return true when you look for the class foo on an element which has the foobar class.

Related articles

The World's Leading Crypto Trading Platform

Get my welcome gifts